//
//  ViewController.swift
//  C010_test_indexed_tableview
//
//  Created by yao_yu on 15/5/31.
//  Copyright (c) 2015年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {

    var tableView = UITableView()
    var datas:[String]!
    var indexes:[String]!
    var resultSearchController = UISearchController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let allNumbers = "100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1500 1500 1500 1500 1500 1500 1500 1500 1500 1500"
        datas = allNumbers.componentsSeparatedByString(" ")
        
        let indexNumbers = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22"
        indexes = indexNumbers.componentsSeparatedByString(" ")
        
        tableView.delegate = self
        tableView.dataSource = self
        
        view.addSubview(tableView)
        
        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            
            return controller
        })()
        view.addSubview(resultSearchController.view)
    }
    
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        
    }
    
    override func viewDidLayoutSubviews() {
        resultSearchController.view.frame  = CGRectMake(0, 20, view.frame.width, 44)
        resultSearchController.searchBar.sizeToFit()
        tableView.frame = CGRectMake(0, 64, view.frame.width, view.frame.height - 64)
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return indexes.count
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        if let datas = self.datas {
//            return datas.count
//        }
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let reuseIdentifier = "cell"
        var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier)
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
        }
        
        cell?.textLabel?.text = datas[indexPath.section]
        
        return cell!
    }
    
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        return indexes
    }
    
       

    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        let temp = indexes as NSArray
        return temp.indexOfObject(title)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

